package com.example.homeworkhelper.faq;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import com.example.homeworkhelper.R;
import com.example.homeworkhelper.faq.request.FaqRequestUtils;
import com.example.homeworkhelper.history.bean.RecordData;
import com.example.homeworkhelper.utils.TransferUtils;
import com.example.homeworkhelper.utils.common.Config;

import java.util.ArrayList;
import java.util.List;

public class FaqActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private FaqDataAdapter adapter;
    private ArrayList<FaqData> faqDataArrayList = new ArrayList<>();
    private FaqOnClickListener onClickListener;
    private Handler handler = new Handler(){
        //接收请求完成消息
        @Override
        public void handleMessage(@NonNull Message msg){
            super.handleMessage(msg);
            String res = (String) msg.obj;
            new TransferUtils(res, FaqData.class, faqDataArrayList);
            System.out.println(faqDataArrayList.size());
            adapter.notifyDataSetChanged();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_faq);
        onClickListener = new FaqOnClickListener();
        recyclerView = findViewById(R.id.faq_rv);
        FaqRequestUtils utils = new FaqRequestUtils();
        utils.setHandler(handler);
        utils.getAllFAQ(Config.HttpUrlHead + "/helper/faq/showAll");
        initView();
        adapter = new FaqDataAdapter(faqDataArrayList, onClickListener, this);
        recyclerView.setAdapter(adapter);
    }

    private void initView() {
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
    }
}